package com.igoal.dto;

import java.io.Serializable;
import java.util.Date;

import org.tuckey.web.filters.urlrewrite.utils.StringUtils;

import com.igoal.entity.GoalStatusEnum;
import com.igoal.entity.UserGoal;
import com.igoal.entity.UserGoalPk;
import com.igoal.service.paging.PageItem;
import com.igoal.utils.CalendarUtils;
import com.igoal.utils.SynchronizeAttributeWithOther;
import com.igoal.utils.SynchronizeDirection;

public class UserGoalDTO extends BaseDTO<UserGoal> implements Serializable, PageItem<Long> {
	private static final long serialVersionUID = -3732721707335802971L;

	private Long userId;
	private String username;
	private Long goalId;
	private String title;
	private Integer userCount;
	private Integer entryCount;
	private String status;
	private Integer percent;
	private String showActionControl = "no";
	private String value;
	private String deadLine;
	private int remainingDate;

	public Long getUserId() {
		return userId;
	}

	public void setUserId(Long userId) {
		this.userId = userId;
	}

	public String getUsername() {
		return username;
	}

	public void setUsername(String username) {
		this.username = username;
	}

	public Long getGoalId() {
		return goalId;
	}

	public void setGoalId(Long goalId) {
		this.goalId = goalId;
	}

	public String getTitle() {
		return title;
	}

	public void setTitle(String title) {
		this.title = title;
	}

	public Integer getUserCount() {
		return userCount;
	}

	public void setUserCount(Integer userCount) {
		this.userCount = userCount;
	}

	public String getStatus() {
		return status;
	}

	public void setStatus(String status) {
		this.status = status;
	}

	public Integer getEntryCount() {
		return entryCount;
	}

	public void setEntryCount(Integer entryCount) {
		this.entryCount = entryCount;
	}

	public Integer getPercent() {
		return percent;
	}

	public void setPercent(Integer percent) {
		this.percent = percent;
	}

	public String getShowActionControl() {
		return showActionControl;
	}

	public void setShowActionControl(String showActionControl) {
		this.showActionControl = showActionControl;
	}
	
	public Long getSortedValue() {
		return this.updateDate;
	}

	public String getValue() {
		return value;
	}

	public void setValue(String value) {
		this.value = value;
	}

	public String getDeadLine() {
		return deadLine;
	}

	public void setDeadLine(String deadLine) {
		this.deadLine = deadLine;
	}

	@SynchronizeAttributeWithOther(direction = SynchronizeDirection.FROM, value = "pk")
	public void initFromPK(Object pk) {
		if (pk != null && pk instanceof UserGoalPk) {
			UserGoalPk userGoalPk = (UserGoalPk) pk;
			this.userId = userGoalPk.getUser().getId();
			this.username = userGoalPk.getUser().getUsername();
			this.goalId = userGoalPk.getGoal().getId();
			this.title = userGoalPk.getGoal().getTitle();
			this.userCount = userGoalPk.getGoal().getUserCount();
			//TODO: write trigger update userCount when delete userGoal
		}
	}

	@SynchronizeAttributeWithOther(direction = SynchronizeDirection.FROM, value = "goalStatus")
	public void initFromGoalStatus(Object goalStatus) {
		this.status = goalStatus != null ? goalStatus.toString() : GoalStatusEnum.THINKING.toString();
	}
	
	
	@SynchronizeAttributeWithOther(direction = SynchronizeDirection.FROM, value="updateDate")
	public void initUpdateDate(Object obj){
		if(obj != null && obj instanceof Date){
			Date date = (Date) obj;
			updateDate = date.getTime();
		}
	}

	@SynchronizeAttributeWithOther(direction = SynchronizeDirection.FROM, value="deadLine")
	public void initDeadLine(Object obj){
		if(obj != null && obj instanceof Date){
			Date date = (Date) obj;
			this.remainingDate = CalendarUtils.getNumberOfCommingDate(date);
			this.deadLine = CalendarUtils.formatDate(date, "dd/MM/yyyy");
		}
	}	
	
	@SynchronizeAttributeWithOther(direction = SynchronizeDirection.TO, value="deadLine")
	public Date copyDeadLine(){
		if(!StringUtils.isBlank(deadLine)){
			Date date = CalendarUtils.parseDate(deadLine, "dd/MM/yyyy");			
			return date;
		}
		
		return new Date();
	}

	public int getRemainingDate() {
		return remainingDate;
	}

	public void setRemainingDate(int remainingDate) {
		this.remainingDate = remainingDate;
	}
}
